 module Features
  MAX_POINTS = 100  
  module ScoringPoints
    
    WEIGHT = 1
    
    class << self
      
      def evaluate_game(dg, domino, dominee)
        dg.play_domino domino, dominee
        old_points = dg.current_player.points
        dg.adjust_score
        
        player = dg.current_player
        my_points = player.points - old_points
        
        if player.points > DominoesGame::TARGET_POINTS
          Features::MAX_POINTS
        else
          
          eo = expected_opp_points(dg.dup)
          puts "my points #{my_points} expected opponent points #{eo} adjusted score #{my_points - eo}"
          my_points - eo
          
        end
        
      end
      
      def expected_opp_points(dg)
        
        dg.players_play = !dg.players_play
        
        player = dg.current_player
        valids = dg.valid_generator(dg.pool.dominoes, dg.board)
        
        values = []
        
        valids.each do |k, v|
          v.each do |l|
        
            dup_game = dg.dup
            prob = dup_game.prob_hash[k.head, k.tail]
            domino = k.dup
            dominee = l.dup
          
            dup_game.play_domino domino, dominee
            old_points = dup_game.current_player.points
            dup_game.adjust_score
            
            
            opp_points = dup_game.current_player.points
            
            if dup_game.current_player.points > DominoesGame::TARGET_POINTS
              opp_points = Features::MAX_POINTS
            end
        
            values << ((opp_points-old_points) * prob)
        
          end
        end
        puts "values #{values}"
        avg = (values.inject(0){|memo,x| memo + x } / values.size).round(4)
        avg
      end
    
    end
  end

end